﻿using System;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Collections;

namespace Texpro
{
    public partial class FMTR602 : Form
    {

        public FMTR602()
        {
            InitializeComponent();
        }

        private void FMTR602_Load(object sender, EventArgs e)
        {
            UseWaitCursor = true;
            lbl_form_title.Text = this.Text;
            load_form();
            UseWaitCursor = false;
        }

        private void clear_fields()
        {
            dt_receipt_date.Value = PCPUB.m_tran_date;
            PCPUB.m_fill_combo(lst_vendor, "NONE");
            lst_vendor_type.SelectedIndex = -1;
            lst_vendor.SelectedIndex = -1;
            txt_receipt_id.Text = String.Empty;
            txt_amount.Text = String.Empty;
            txt_remarks.Text = String.Empty;
            switch (PCPUB.m_entry_form_mode)
            {
                case "ADDNEW":
                    dt_receipt_date.Enabled = false;
                    break;
                case "EDIT":
                    dt_receipt_date.Enabled = false;
                    fill_form_data(PCPUB.m_record_date, PCPUB.m_record_id);
                    break;
            }
        }

        private void fill_form_data(string p_date, string p_id)
        {
            String strQuery = String.Empty;
            OleDbConnection sqlConn = new OleDbConnection();
            OleDbCommand sqlComm = new OleDbCommand();
            OleDbDataReader sqlRead;// = new OleDbDataReader();
            DateTime strReturn = DateTime.Now;
            //           
            sqlConn.ConnectionString = PCPUB.m_oledb_connection.ToString();
            sqlConn.Open();
            //
            strQuery = String.Empty;
            strQuery += " SELECT CONVERT(DATETIME,[receipt_date]) receipt_date,";
            strQuery += " [receipt_id] receipt_id,";
            strQuery += " [vendor_type_id] vendor_type_id,";
            strQuery += " [vendor_id] vendor_id,";
            strQuery += " [amount] amount,";
            strQuery += " [remarks] remarks";
            strQuery += " FROM [tis_receipt]";
            strQuery += " WHERE [receipt_date] = '" +  PCPUB.m_get_sql_date(p_date) + "'";
            strQuery += " AND [receipt_id] = '" + p_id + "'";
            //
            sqlComm.Connection = sqlConn;
            sqlComm.CommandText = strQuery.ToString();
            sqlRead = sqlComm.ExecuteReader();
            //
            if (sqlRead.Read())
            {
                dt_receipt_date.Value = (DateTime)sqlRead["receipt_date"];
                txt_receipt_id.Text = sqlRead["receipt_id"].ToString();
                lst_vendor_type.SelectedIndex = lst_vendor_type.FindStringExact(PCPUB.m_get_vendor_type_name(sqlRead["vendor_type_id"].ToString()));
                lst_vendor.SelectedIndex = lst_vendor.FindStringExact(PCPUB.m_get_vendor_name(sqlRead["vendor_id"].ToString(), sqlRead["vendor_type_id"].ToString()));
                txt_amount.Text = sqlRead["amount"].ToString();
                txt_remarks.Text = sqlRead["remarks"].ToString();
            }
            //
            strQuery = String.Empty;
            //
            sqlRead.Close();
            sqlConn.Close();
            sqlRead.Dispose();
            sqlComm.Dispose();
            sqlConn.Dispose();
        }

        private void load_form()
        {
            PCPUB.m_fill_combo(lst_vendor_type, "VENDOR_TYPE");
            clear_fields(); 
        }

        private bool save_form()
        {
            try
            {
                String strQuery = String.Empty;
                ArrayList alQueries = new ArrayList();
                OleDbConnection sqlConn = new OleDbConnection();
                OleDbCommand sqlComm = new OleDbCommand();
                //
                String receipt_date = String.Empty;
                String receipt_id = String.Empty;
                String user_id = String.Empty;
                String user_datetime = String.Empty;
                String vendor_type_id = String.Empty;
                String vendor_id = String.Empty;
                String amount = String.Empty;
                String remarks = String.Empty;
                //
                //
                user_id = "'" + PCPUB.m_user_id + "'";
                user_datetime = "'" + PCPUB.m_get_sql_datetime() + "'";
                vendor_id = "'" + ((m_combo_item)lst_vendor.SelectedItem).combo_code.ToString() + "'";
                vendor_type_id = "'" + ((m_combo_item)lst_vendor_type.SelectedItem).combo_code.ToString() + "'";
                amount = txt_amount.Text;
                remarks = "'" + txt_remarks.Text + "'";
                //
                switch (PCPUB.m_entry_form_mode)
                {
                    case "ADDNEW":
                        receipt_id = "'" + PCPUB.m_get_next_sequence("RECEIPT",dt_receipt_date) + "'";
                        receipt_date = "'" + PCPUB.m_get_sql_date(dt_receipt_date) + "'";
                        strQuery = String.Empty;
                        strQuery += "INSERT INTO [tis_receipt] (";
                        strQuery += "[receipt_date],";
                        strQuery += "[receipt_id],";
                        strQuery += "[user_id],";
                        strQuery += "[user_datetime],";
                        strQuery += "[vendor_type_id],";
                        strQuery += "[vendor_id],";
                        strQuery += "[amount],";
                        strQuery += "[remarks]";
                        strQuery += ") VALUES (";
                        strQuery += receipt_date + ",";
                        strQuery += receipt_id + ",";
                        strQuery += user_id + ",";
                        strQuery += user_datetime + ",";
                        strQuery += vendor_type_id + ",";
                        strQuery += vendor_id + ",";
                        strQuery += amount + ",";
                        strQuery += remarks + ");";
                        alQueries.Add(strQuery);
                        break;
                    case "EDIT":
                        receipt_id = "'" + PCPUB.m_record_id + "'";
                        receipt_date = "'" + PCPUB.m_get_sql_date(PCPUB.m_record_date) + "'";
                        strQuery = String.Empty;
                        strQuery += " UPDATE [tis_receipt] SET";
                        strQuery += " [user_id] = " + user_id + ",";
                        strQuery += " [user_datetime] = " + user_datetime + ",";
                        strQuery += " [vendor_type_id] = " + vendor_type_id + ",";
                        strQuery += " [vendor_id] = " + vendor_id + ",";
                        strQuery += " [amount] = " + amount + ",";
                        strQuery += " [remarks] = " + remarks;
                        strQuery += " WHERE [receipt_date] = " + receipt_date;
                        strQuery += " AND [receipt_id] = " + receipt_id + ";";
                        alQueries.Add(strQuery);
                        break;
                }
                //
                sqlConn.ConnectionString = PCPUB.m_oledb_connection.ToString();
                sqlConn.Open();
                sqlComm.Connection = sqlConn;
                for (int i =0;i<alQueries.Count; i++)
                {
                    sqlComm.CommandText = alQueries[i].ToString();
                    sqlComm.ExecuteNonQuery();
                }
                sqlConn.Close();
                PCPUB.m_receipt_id = receipt_id;
                //
                sqlComm.Dispose();
                sqlConn.Dispose();
                return true;
            }
            catch (Exception e)
            {
                PCMSG.Show(e.Message);
                return false;
            }
        }

        private bool validate_fields()
        {
            if (lst_vendor_type.SelectedIndex == -1)
            {
                PCMSG.ShowError("Vendor Type field should not be empty.");
                lst_vendor_type.Focus();
                return false;
            }
            else if (lst_vendor.SelectedIndex == -1)
            {
                PCMSG.ShowError("Vendor field should not be empty.");
                lst_vendor_type.Focus();
                return false;
            }
            else if (txt_amount.Equals(String.Empty))
            {
                PCMSG.ShowError("Amount field should not be empty.");
                lst_vendor_type.Focus();
                return false;
            }
            //else if (txt_remarks.Equals(String.Empty))
            //{
            //    PCMSG.ShowError("Remarks field should not be empty.");
            //    txt_remarks.Focus();
            //    return false;
            //}
            return true;
        }

        private void but_save_Click(object sender, EventArgs e)
        {
            UseWaitCursor = true;
            if (validate_fields() == true)
            {
                if (save_form() == true)
                {
                    UseWaitCursor = false;
                    PCMSG.Show("Receipt saved successfully. Receipt Id is " + PCPUB.m_receipt_id.ToString() + ".","Receipt");
                    UseWaitCursor = true;
                    PCPUB.m_receipt_id = String.Empty;
                    clear_fields();
                    lst_vendor_type.Focus();
                    switch (PCPUB.m_entry_form_mode)
                    {
                        case "ADDNEW":
                            break;
                        case "EDIT":
                            this.Close();
                            break;
                    }

                }
            }
            UseWaitCursor = false;
        }

        private void but_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lst_vendor_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox comboBox = (ComboBox)sender;
            String tmp = String.Empty;
            try
            {
                tmp = ((m_combo_item)comboBox.SelectedItem).combo_code.ToString();
            }
            catch
            {
                tmp = "NONE";
            }
            PCPUB.m_fill_combo(lst_vendor, tmp);
        }

        private void lst_vendor_type_SelectionChangeCommitted(object sender, EventArgs e)
        {
            
        }

        private void lst_vendor_type_Click(object sender, EventArgs e)
        {
            
        }

        private void text_id_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsLetterOrDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back))
            {
                e.Handled = true;
            }
        }

        private void text_gen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsLetterOrDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space || e.KeyChar == '.'))
            {
                e.Handled = true;
            }
        }

        private void text_int_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back))
            {
                e.Handled = true;
            }
        }

        private void text_dec_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == '.'))
            {
                e.Handled = true;
            }
        }


    }

}